﻿using Hidistro.Membership.Context;
using Hidistro.UI.Common.Controls;
using JSShop.Model;
using Juse.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using wnxd.javascript;
using Hidistro.Core;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class LimitBuyList : AscxTemplatedWebControl
    {
        private ProductBrowser ProductBrowser = new ProductBrowser();
        private Repeater RList;
        private Dictionary<int, Hishop_CountDown> map;
        protected override void AttachChildControls()
        {
            this.RList = (Repeater)this.FindControl("RList");
            this.RList.ItemDataBound += RList_ItemDataBound;
            List<Hishop_CountDown> list = ProductBrowser.GetCountDownList();
            if (list != null && list.Count > 0)
            {
                list = list.OrderByDescending(t => t.CountDownId).ToList();
                this.map = new Dictionary<int, Hishop_CountDown>();
                for (int i = 0; i < this.MaxNum || i < list.Count; i++)
                {
                    Hishop_CountDown item = list[i];
                    if (!this.map.ContainsKey(item.ProductId))
                    {
                        this.map.Add(item.ProductId, item);
                    }
                }
                
                aspnet_Members member = null;
                Member m = HiContext.Current.User as Member;
                if (m != null) member = new json(m).TryConvert(typeof(aspnet_Members)) as aspnet_Members;
                List<vw_Hishop_BrowseProductList> hbList = ProductBrowser.GetVistiedProducts(member, this.map.Keys.ToList());
                if (hbList!=null && hbList.Count>6)
                {
                    hbList = hbList.Take(6).ToList();
                }
                this.RList.DataSource = hbList;
                this.RList.DataBind();
            }
        }

        private void RList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                vw_Hishop_BrowseProductList info = (vw_Hishop_BrowseProductList)e.Item.DataItem;
                Hishop_CountDown cd = this.map[info.ProductId];
                HtmlImage Image = (HtmlImage)e.Item.FindControl("Image");
                Image.Src = Globals.GetImgUrl(cd.Image);
                Literal Content = (Literal)e.Item.FindControl("Content");
                Content.Text = cd.Content;
                Literal CountDownPrice = (Literal)e.Item.FindControl("CountDownPrice");
                CountDownPrice.Text = cd.CountDownPrice.ToString("F2");
                Literal Time = (Literal)e.Item.FindControl("Time");
                Time.Text = (cd.EndDate.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds.ToString();
                //if (ProductBrowser.GetSentFreight(cd.CountDownPrice) == null)
                //{
                //    Control baoyou = e.Item.FindControl("baoyou");
                //    baoyou.Visible = false;
                //    baoyou = e.Item.FindControl("baoyou2");
                //    baoyou.Visible = false;
                //}
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_GoodsList/LimitBuyList.ascx";
            }
            base.OnInit(e);
        }
        public int MaxNum { get; set; }
    }
}
